<?php

namespace catchAdmin\business\controller;

use catcher\base\CatchRequest as Request;
use catcher\CatchResponse;
use catcher\base\CatchController;
use catchAdmin\business\model\Shipping as ShippingModel;
use think\Response;
use catcher\CatchAuth;
use think\facade\Db;
class Shipping extends CatchController
{
    
    protected $shippingModel;
    
    /**
     *
     * @time 2022/02/18 14:36
     * @param ShippingModel $shippingModel
     * @return mixed
     */
    public function __construct(ShippingModel $shippingModel)
    {
        $this->shippingModel = $shippingModel;
    }
    
    /**
     *
     * @time 2022/02/18 14:36
     * @return Response
     * 海进海出列表
     */
    public function index(CatchAuth $auth)
    {
        $user = $auth->user();
        return CatchResponse::paginate($this->shippingModel->getList($user['tenant_id']));
    }

    /**
     *
     * @time 2022/02/15 11:41
     * @throws \think\db\exception\DbException
     * @return \think\response\Json
     */
   public function create(CatchAuth $auth,Request $request)
    {
         $params = $request->filterEmptyField()->post();
         Db::startTrans();
         try {
             $user = $auth->user();
             $params['tenant_id'] = $user['tenant_id'];
             $params['job_number'] = 'SE'.date('YmdHis');

             if(!empty($params['declaration'])) {
                 //报关表
                 foreach ($params['declaration'] as $key => $value){
                      unset($value['key']);
                      $value['job_number'] =  $params['job_number'];
                      $value['created_at'] = time();
                      $value['updated_at'] = time();
                      $value['tenant_id'] = $user['tenant_id'];
                      DB::name('db_declare_document')->insert($value);
                 }
             }

             if(!empty($params['transport'])) {
                 //运输表
                 foreach ($params['transport'] as $key => $value){
                      unset($value['key']);
                      $value['job_number'] =  $params['job_number'];
                      $value['created_at'] = time();
                      $value['updated_at'] = time();
                      $value['tenant_id'] = $user['tenant_id'];
                      DB::name('db_transport')->insert($value);
                 }
             }

             //其他信息表
             if(!empty($params['otherinfo'])) {
                 foreach ($params['otherinfo'] as $key => $value){
                     $data['job_number']=$params['job_number'];
                     $data['creator_id']=$data['tenant_id']=$user['id'];
                     $data['created_at']=time();
                     $data['updated_at']=time();
                     $data[$key]=$value;
                 }
                 Db::table('db_otherinfo')->insert($data);
             }

             unset($params['declaration']);
             unset($params['transport']);
             $this->shippingModel->storeBy($params);
             Db::commit();
             return CatchResponse::success('', '新建成功');
        } catch (\Exception $exception) {
             Db::rollback();
             return CatchResponse::fail('新建失败');
        }
    }
    
    /**
     *
     * @time 2022/02/18 14:36
     * @param Request $request
     * @return Response
     */
   public function update(Request $request)
    {
         $params = $request->filterEmptyField()->post();
         Db::startTrans();
         try {
             if(!empty($params['declaration'])) {
                 //报关表
                 foreach ($params['declaration'] as $key => $value) {
                     unset($value['key']);
                     $value['updated_at'] = time();
                     if(!empty($value['id'])) {
                         DB::name('db_declare_document')->where('id', $value['id'])->update($value);
                     } else{
                         $value['created_at'] = time();
                         $value['job_number'] = $params['job_number'];
                         DB::name('db_declare_document')->insert($value);
                     }
                 }
             }

             if(!empty($params['transport'])) {
                 //运输表
                 foreach ($params['transport'] as $key => $value) {
                     unset($value['key']);
                     $value['updated_at'] = time();
                     if(!empty($value['id'])) {
                         unset($value['transport_company_name']);
                         DB::name('db_transport')->where('id', $value['id'])->update($value);
                     } else {
                        $value['created_at'] = time();
                        $value['job_number'] = $params['job_number'];
                         unset($value['transport_company_name']);
                        DB::name('db_transport')->insert($value);
                     }
                 }
             }

             //其他信息表
             if(!empty($params['otherinfo'])) {
                 //必传其他信息表的id
                 $params['otherinfo']['updated_at']=time();
                 Db::table('db_otherinfo')->update($params['otherinfo']);
             }

             unset($params['declaration']);
             unset($params['transport']);
             $this->shippingModel->updateBy($params['id'],$params);
             Db::commit();
             return CatchResponse::success('', '编辑成功');
        } catch (\Exception $exception) {
             Db::rollback();
             return CatchResponse::fail('编辑失败');
        }
    }
    
    /**
     *
     * @throws \think\db\exception\DbException
     * @return \think\response\Json
     */
     public function delete(Request $request)
    {
        $id = $request->post('id');
         Db::startTrans();
         try {
             $job_number = $this->shippingModel->where('id',$id)->value('job_number');
             $data['deleted_at'] = time();
             DB::name('db_declare_document')->where('job_number','in',$job_number)->update($data);
             DB::name('db_transport')->where('job_number','in',$job_number)->update($data);
             //其他信息表软删除
             Db::table('db_otherinfo')->where('job_number','in',$job_number)->update($data);

             $this->shippingModel->deleteBy([$id]);
             Db::commit();
             return CatchResponse::success('', '删除成功');
        } catch (\Exception $exception) {
             Db::rollback();
             return CatchResponse::fail('删除失败');
        }
    }

    /**
     * 打印
     * @param Request $request
     * @return \think\response\Json
     */
    public function print(Request $request){
        $id=$request->filterEmptyField()->post('id');
        //以id获取一条空进空出信息
        $data=$this->shippingModel->where('id',$id)->find();
        //以工作号获取运输明细数组
        $transports=Db::name('db_transport')->where('job_number','in',$data['job_number'])->select();
        //以创建人id获取用户名
        $creator=Db::name('users')->field('username')->where('creator_id',$data['creator_id'])->find();
        //获取委托单位名称
        $accountname=Db::name('db_provider')->field('account_name')->where('id',$data['entrust_company'])->find();
        //获取经营单位名称
        $accountname1=Db::name('db_provider')->field('account_name')->where('id',$data['management_company'])->find();
        //获取内部类型
        $insidetype=Db::name('db_inside_type')->field('name')->where('id',$data['inside_type'])->find();
        $data['inside_type']=$insidetype['name'];
        $data['management_company']=$accountname1['account_name'];
        $data['entrust_company']=$accountname['account_name'];
        $data['transports']=$transports;
        $data['creator']=$creator['username'];
        return CatchResponse::success($data,'查询成功');
    }

}